﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using PSE.Deployment.ConfigurationManager.Controls;
using PSE.Deployment.Engine.Enums;
using PSE.Deployment.Engine.Interfaces;

namespace PSE.Deployment.SetupBase.UI
{
    public class ExecutionStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is IStepGroup)
            {
                IStepGroup a = (IStepGroup)value;

                switch (a.ExecutionStatus)
                {
                    case StepExecutionStatus.Canceled:
                        return "Cancelado";
                    case StepExecutionStatus.Finished:
                        if (a.IgnoredSteps.Count == 0)
                            return "Finalizado com sucesso";
                        else
                            return "Finalizado com erros";
                    default:
                        return string.Empty;
                }
            }
            return "Error";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public partial class SetupResult : BaseWindow
    {
        private string _directoryName;
        public Parameter.Enums.SetupMode SetupMode { get; set; }

        public List<IStepGroup> StepGroups
        {
            get { return (List<IStepGroup>)GetValue(StepGroupsProperty); }
            set { SetValue(StepGroupsProperty, value); }
        }

        public static readonly DependencyProperty StepGroupsProperty =
            DependencyProperty.Register("StepGroups", typeof(List<IStepGroup>), typeof(SetupResult), new UIPropertyMetadata(null));

        public SetupResult()
        {
            this.InitializeComponent();
            this.DataContext = this;
            this.Loaded += new RoutedEventHandler(SetupResult_Loaded);
        }

        void SetupResult_Loaded(object sender, RoutedEventArgs e)
        {
            _directoryName = Path.Combine(SetupMainFactory.GetInstance().Parameter.InstalationSource, @"Guides\pt-br\");

            if (!Directory.Exists(Path.GetDirectoryName(_directoryName)))
            {
                TxtLinkManual.Visibility = Visibility.Collapsed;
            }

            TxtTitleInstall.Visibility = (SetupMode == Parameter.Enums.SetupMode.Install || SetupMode == Parameter.Enums.SetupMode.HotFix || SetupMode == Parameter.Enums.SetupMode.ServicePack) ? Visibility.Visible : Visibility.Collapsed;
            TxtTitleUninstall.Visibility = SetupMode == Parameter.Enums.SetupMode.Uninstall ? Visibility.Visible : Visibility.Collapsed;
            TxtTitleRepair.Visibility = SetupMode == Parameter.Enums.SetupMode.Repair ? Visibility.Visible : Visibility.Collapsed;
            TxtTitleChange.Visibility = SetupMode == Parameter.Enums.SetupMode.Change ? Visibility.Visible : Visibility.Collapsed;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationAction = StepNavigationAction.Next;
            this.Close(false);
        }

        private void InstallationGuide_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = Path.GetDirectoryName(_directoryName);
            process.Start();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == System.Windows.Input.Key.Enter)
            {
                this.Close();
            }
        }
    }
}